package commons.resources;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

import commons.resources.CommonsEnum.ICONS;

public class CommonsPluginResources {
    private static String ID = "commons";
    // file.separator File separator ("/" on UNIX)
    public static String FILE_SEPARATOR = System.getProperty("file.separator");
    // path.separator Path separator (":" on UNIX)
    public static String LINE_SEPARATOR = System.getProperty("line.separator");
    // line.separator Line separator ("\n" on UNIX)
    public static String PATH_SEPARATOR = System.getProperty("path.separator");

    private static ImageRegistry _imageRegistry;

    public static ImageRegistry getImageRegistry() {
	if (_imageRegistry == null) {
	    _imageRegistry = new ImageRegistry();
	    _imageRegistry.put(ID + "." + ICONS.CHECKED.toString(), getImageByFileName(ID, ICONS.CHECKED.getValue()));
	    _imageRegistry.put(ID + "." + ICONS.UNCHECKED.toString(), getImageByFileName(ID, ICONS.UNCHECKED.getValue()));
	    _imageRegistry.put(ID + "." + ICONS.REQUIRED.toString(), getImageByFileName(ID, ICONS.REQUIRED.getValue()));
	}
	return _imageRegistry;
    }

    private static Image getImageByFileName(String pluginId, String fileName) {
	ImageDescriptor descriptor = null;
	try {
	    URL installURL = FileLocator.resolve(Platform.getBundle(pluginId).getEntry("/"));
	    URL url = new URL(installURL, "icons/" + fileName);
	    descriptor = ImageDescriptor.createFromURL(url);
	} catch (MalformedURLException eURL) {
	    // Logger.error(eURL);
	} catch (IOException x) {
	    // Logger.error(x);
	}
	if (descriptor == null) {
	    descriptor = ImageDescriptor.getMissingImageDescriptor();
	}
	return descriptor.createImage();
    }

    /**
     * Returns the image in CORE's image registry with the given key, or
     * <code>null</code> if none. Convenience method equivalent to
     * 
     * <pre>
     * CorePluginResources.getImageRegistry().get(key)
     * </pre>
     * 
     * @param key
     *            the key
     * @return the image, or <code>null</code> if none
     */
    public static Image getImage(String key) {
	Image image = getImageRegistry().get(ID + "." + key);
	if (image == null) {
	    image = getImageByFileName(ID, key);
	    getImageRegistry().put(ID + "." + key, image);
	}
	return image;
    }

    /**
     * Returns the image in CORE's image registry with the given key, or
     * <code>null</code> if none. Convenience method equivalent to
     * 
     * <pre>
     * CorePluginResources.getImageRegistry().get(key)
     * </pre>
     * 
     * @param pluginId
     * @param key
     *            the key
     * @return the image, or <code>null</code> if none
     */
    public static Image getImage(String pluginId, String key) {
	Image image = getImageRegistry().get(pluginId + "." + key);
	if (image == null) {
	    image = getImageByFileName(pluginId, key);
	    getImageRegistry().put(pluginId + "." + key, image);
	}
	return image;
    }
}
